package com.example.testproj.udp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.example.testproj.R;
import com.example.testproj.utils.ThreadUtil;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class UdpTestActivity extends AppCompatActivity {
    private static final String TAG = "UdpTestActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_udp_test);
        ThreadUtil.getThreadUtil().submit(new FutureTask(new Callable() {
            @Override
            public Object call() throws Exception {
                DatagramSocket socket = new DatagramSocket(4567);
                byte data[] = new byte[1024];
                DatagramPacket packet = new DatagramPacket(data, data.length);
                DatagramSocket sendSocket = new DatagramSocket();
                while (true){

                    socket.receive(packet);
                    String content = new String(packet.getData(), packet.getOffset(), packet.getLength());
                    Log.d(TAG, "call: " + content);

//                    socket.send();
//
//                    DatagramPacket sendPacket = new DatagramPacket(data, data.length,socket.getInetAddress(),4567);
//                    sendPacket.setData("我是服务端发来的".getBytes("utf-8"));
//                    sendSocket.send(sendPacket);
                    Log.d(TAG, "发送完毕 " );
                }


//                return null;
            }
        }));
    }
}
